import axios from 'axios'
import { Message } from 'element-ui'

const request = axios.create({
  baseURL: import.meta.env.VITE_BASEURL || '/',
  timeout: 1000 * 20,
})

request.interceptors.request.use(
  config => {
    return config
  },
  error => {
    console.log(error)
    return Promise.reject(error)
  }
)

request.interceptors.response.use(
  ({ data }) => {
    const { code, msg, desc } = data
    if (code !== 200) {
      if(code === 401) {
        Message.error(msg || desc || '用户验证失败,请重新登录!')
      } else {
        Message.warning(msg || desc || '服务异常')
      }
      return Promise.reject(data)
    }
    return data
  },
  error => {
    Message.error(error.message)
    return Promise.reject(error)
  }
)

export default request

// axios.request(config)
// axios.get(url[, config])
// axios.delete(url[, config])
// axios.head(url[, config])
// axios.post(url[, data[, config]])
// axios.put(url[, data[, config]])
// axios.patch(url[, data[, config]])
